package com.examples.calendar;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;

public class MouseClickListener implements MouseListener {
	
	Canvas canvas;
	GC gc;
	Rectangle rectangleToBeFocused;
	Rectangle oldRectangleToBeFocused;
	Rectangle oldRectangle;
	Rectangle oldBounds;
	Rectangle newBounds;
	Rectangle alreadyPainted;
	
	
	public MouseClickListener(Canvas canvas) {
		this.canvas = canvas;
	}

	@Override
	public void mouseDoubleClick(MouseEvent e) {		
	}

	@SuppressWarnings("deprecation")
	@Override
	public void mouseDown(MouseEvent e) {
		newBounds = e.display.getActiveShell().getBounds();
		Rectangle rectangle = e.display.getActiveShell().getClientArea();
		Point point = new Point(e.x, e.y);
		
		final int totalWidth = rectangle.width;
		final int totalHeight = rectangle.height;
		final int widthOfCell = (totalWidth - 5) / 7;
		final int heightOfCell = (totalHeight - 20) / 5;
		
		if(e.y < 20){
		}
		Rectangle newRectangle = new Rectangle(0, 20, widthOfCell, heightOfCell);
		
		if(RectangleHolder.getRectangle() != null){
			alreadyPainted = e.display.map(e.display.getActiveShell(), null, RectangleHolder.getRectangle());	
		}
		
		int leftToRightCount = 0;
		@SuppressWarnings("unused")
		int topToDownCount = 0;
		
		while(newRectangle.x < totalWidth && newRectangle.y < totalHeight){
			if(newRectangle.contains(point)){
				if(oldRectangle == newRectangle){
				}
				else{
					gc = new GC(e.display);
					rectangleToBeFocused = e.display.map(e.display.getActiveShell(), null, newRectangle);
					gc.setXORMode(true);
					gc.setBackground(e.display.getSystemColor(SWT.COLOR_BLUE));
					gc.fillRectangle(rectangleToBeFocused.x + 1, rectangleToBeFocused.y + 1, rectangleToBeFocused.width-1,rectangleToBeFocused.height-1);
					
					
					
					if (alreadyPainted != null){
						gc.fillRectangle(alreadyPainted.x, alreadyPainted.y, alreadyPainted.width,alreadyPainted.height);	
						RectangleHolder.setRectangle(null);
						alreadyPainted = null;
					}					
					
					
					if(oldBounds != null && (oldBounds.width != newBounds.width || oldBounds.height != newBounds.height)){
						oldRectangle = null;
					}
					oldBounds = newBounds;
					if(oldRectangle != null){
						oldRectangleToBeFocused = e.display.map(e.display.getActiveShell(), null, oldRectangle);
						gc.fillRectangle(oldRectangleToBeFocused.x + 1, oldRectangleToBeFocused.y + 1, oldRectangleToBeFocused.width-1,oldRectangleToBeFocused.height-1);
						gc.dispose();
					}
					oldRectangle = newRectangle;
					gc.dispose();
					break;	
				}				
			}
			else{
				newRectangle.x += widthOfCell;
				leftToRightCount++;
				if(leftToRightCount == 7){
					newRectangle.x = 0;
					newRectangle.y += heightOfCell;
					leftToRightCount = 0;
					topToDownCount++;
				}
			}
		}
	}
	@Override
	public void mouseUp(MouseEvent e) {
	}
}